<?php

namespace App\Http\Controllers\Root;

use App\Http\Controllers\Controller;
use App\Http\Requests\NewsRequest;
use App\Repositories\NewsRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;


class NewsController extends Controller
{
    /** @var  NewsRepository */
    private $newsRepository;

    public function __construct(NewsRepository $newsRepo)
    {
        $this->newsRepository = $newsRepo;
    }

    /**
     * Display a listing of the News.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $where = [];
        $news = [];
        $this->newsRepository->pushCriteria(new RequestCriteria($request));
        if (!empty($request->title)) {
            $where['title'] = $request->title;
        }
        $order_by = ["column" => "created_at", "direction" => "desc"];
        $news = $this->newsRepository->getResult(15, $where, $order_by);
        return $this->r_view('root.news.index', ['news' => $news]);
    }


    /**
     * Store a newly created News in storage.
     *
     * @param NewsRequest $request
     *
     * @return Response
     */
    public function store(NewsRequest $request)
    {
        $input = $request->all();
        if ($input['fb-id'] != 0) {
            $news = $this->newsRepository->findWithoutFail($input['fb-id']);
            if (empty($news)) {
                return $this->sendError(0, '记录已删除');
            }
            $this->newsRepository->update($input, $input['fb-id']);
            return $this->sendResponse(1, '更新成功');
        } else {
            $this->newsRepository->create($input);
            return $this->sendResponse(1, '添加成功');
        }
    }

    /**
     * Display the specified News.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $news = $this->newsRepository->findWithoutFail($id);


        return $this->r_view('root.news.show')->with('news', $news);
    }

    /**
     * Show the form for editing the specified News.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        parent::getCategory();
        $data['category'] = $this->_vd['category'];
        $data['news'] = [];
        if (!empty($id)) {
            $data['news'] = $this->newsRepository->findWithoutFail($id);
        }
        return $this->r_view('root.news.edit', $data);
    }

    /**
     * Remove the specified News from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $news = $this->newsRepository->findWithoutFail($id);
        if (empty($news)) {
            return $this->sendError(0, '该记录已删除');
        }
        $this->newsRepository->delete($id);
        return $this->sendResponse(1, '删除成功');
    }

    public function getCategoryName($id)
    {
        parent::getCategory();
        if (isset($this->_vd['category'][$id])) {
            return str_replace('-', '', $this->_vd['category'][$id]);
        }
        return '';

    }
}
